package game

import (
	"context"
	"qm_system_server/server/global"
	"qm_system_server/server/model/common/response"
	"qm_system_server/server/proto/grpc/soft"

	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"google.golang.org/grpc/status"
)

type QmSoftApi struct {
}

// CreateQmSoft 创建软件记录
// @Tags QmSoft
// @Summary 创建软件记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body game.QmSoft true "创建软件记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /qmSoft/createQmSoft [post]
func (qmSoftApi *QmSoftApi) CreateQmSoft(c *gin.Context) {
	type request struct {
		SoftName  string `json:"soft_name" form:"soft_name"`   // 软著名称
		SoftHash  string `json:"soft_hash" form:"soft_hash"`   // 软著hash
		IssueDate int64  `json:"issue_date" form:"issue_date"` // 备案日期
		Type      int64  `json:"type" form:"type"`             // 类型
		Main      string `json:"main" form:"main"`             // 主体
	}
	var req request
	_ = c.ShouldBindJSON(&req)

	if req.SoftHash == "" || req.SoftName == "" {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	client := soft.NewSoftServiceClient(global.ServerGrpc)

	_, err := client.Create(context.Background(), &soft.CreateRequest{
		Soft: &soft.Soft{
			SoftName:  req.SoftName,
			SoftHash:  req.SoftHash,
			IssueDate: req.IssueDate,
			Type:      req.Type,
			Main:      req.Main,
		},
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteQmSoft 删除软件记录
// @Tags QmSoft
// @Summary 删除软件记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body game.QmSoft true "删除软件记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /qmSoft/deleteQmSoft [delete]
func (qmSoftApi *QmSoftApi) DeleteQmSoft(c *gin.Context) {
	ID := c.Query("id")

	//转成int64
	id := cast.ToInt64(ID)

	client := soft.NewSoftServiceClient(global.ServerGrpc)

	_, err := client.Delete(context.Background(), &soft.DeleteRequest{
		Id: id,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("删除成功", c)
}

// UpdateQmSoft 更新软件记录
// @Tags QmSoft
// @Summary 更新软件记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body game.QmSoft true "更新软件记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /qmSoft/updateQmSoft [put]
func (qmSoftApi *QmSoftApi) UpdateQmSoft(c *gin.Context) {
	type request struct {
		Id        int64  `json:"id" form:"id"`
		SoftName  string `json:"soft_name" form:"soft_name"`   // 软著名称
		SoftHash  string `json:"soft_hash" form:"soft_hash"`   // 软著hash
		IssueDate int64  `json:"issue_date" form:"issue_date"` // 备案日期
		Type      int64  `json:"type" form:"type"`             // 类型
		Main      string `json:"main" form:"main"`             // 主体
	}

	var req request

	_ = c.ShouldBindJSON(&req)

	if req.Id == 0 || req.SoftHash == "" {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	client := soft.NewSoftServiceClient(global.ServerGrpc)

	_, err := client.Update(context.Background(), &soft.UpdateRequest{
		Soft: &soft.Soft{
			Id:        req.Id,
			SoftName:  req.SoftName,
			SoftHash:  req.SoftHash,
			IssueDate: req.IssueDate,
			Type:      req.Type,
			Main:      req.Main,
		},
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithMessage("更新成功", c)
}

// FindQmSoft 用id查询软件记录
// @Tags QmSoft
// @Summary 用id查询软件记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query game.QmSoft true "用id查询软件记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /qmSoft/findQmSoft [get]
func (qmSoftApi *QmSoftApi) FindQmSoft(c *gin.Context) {
	type request struct {
		Id int64 `json:"id" form:"id"`
	}

	var req request

	c.ShouldBindQuery(&req)

	//判断不能为空
	if req.Id == 0 {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	client := soft.NewSoftServiceClient(global.ServerGrpc)
	getResp, err := client.Get(context.Background(), &soft.GetRequest{
		Id: req.Id,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(getResp.Soft, c)
}

// GetQmSoftList 分页获取软件记录列表
// @Tags QmSoft
// @Summary 分页获取软件记录列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query gameReq.QmSoftSearch true "分页获取软件记录列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /qmSoft/getQmSoftList [get]
func (qmSoftApi *QmSoftApi) GetQmSoftList(c *gin.Context) {
	type request struct {
		Page     int64  `json:"page" form:"page"`
		PageSize int64  `json:"page_size" form:"page_size"`
		SoftName string `json:"soft_name" form:"soft_name"`
		SoftHash string `json:"soft_hash" form:"soft_hash"`
		Type     int64  `json:"type" form:"type"` // 类型
		Main     string `json:"main" form:"main"` // 主体
	}

	var req request

	_ = c.ShouldBindQuery(&req)

	client := soft.NewSoftServiceClient(global.ServerGrpc)

	softs, err := client.List(context.Background(), &soft.ListRequest{
		Page:     req.Page,
		PageSize: req.PageSize,
		SoftName: req.SoftName,
		SoftHash: req.SoftHash,
		Type:     req.Type,
		Main:     req.Main,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(response.PageResult{
		List:  softs.Soft,
		Total: softs.Total,
		Page:  int(req.Page),
	}, c)
}
